package com.jchen.system.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.jchen.common.annotation.TestAnnotation;
import com.jchen.common.util.R;
import com.jchen.common.entity.UserInfo;
import com.jchen.system.feign.UserFeign;
import com.jchen.system.service.IFileService;
import com.jchen.system.service.IUserInfoService;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Description
 * @Author jchen
 * @Date 2023/12/6 10:55
 */
@Api(value = "哈喽沃德",tags = "哈喽沃德")
@RefreshScope
@RestController
@RequestMapping("/user")
public class HelloWordController {

    @Value("${user.id}")
    private String userId;

    @Value("${user.name}")
    private String userName;

    @Autowired
    UserFeign userFeign;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IFileService fileService;

    /**
     * sentinel流控qps设置为1
     * @return
     */
    @ApiOperation("sentinel流控qps设置为1")
    @GetMapping("/getUserConfigTest")
    @SentinelResource(value = "getUserConfigResource",blockHandler = "getUserConfigHandler")
    public String getUserConfigTest(){
        System.out.println("name:"+userName);
        return userId != null ? userId : "未获取到配置";
    }


    @ApiOperation("seata事务")
    @GetMapping("/testFeiApi")
    @GlobalTransactional //开启seata事务
    @TestAnnotation //自定义注解
    public String testFeiApi(){
        UserInfo userInfo = new UserInfo();
        userInfo.setId((long) 2);
        userInfo.setUsername("cjack");
        userInfoService.saveUserInfo(userInfo);
        return userFeign.testFeiApi("1");
    }

    /**
     *
     * @param multipartFile 资源
     * @return filename
     */
    @ApiOperation("minio上传文件")
    @PostMapping("/upload")
    public R upload(@RequestParam("file") MultipartFile[] multipartFile) {
        return fileService.uploadFile(multipartFile);
    }

    /**
     * 限流后续操作方法
     * @return
     */
    public static String getUserConfigHandler(BlockException e){
        String msg = "不好意思，前方拥挤，请您稍后再试";
        return msg;
    }
}
